const fs = require('fs');
const path = require('path');

// 指定要搜索的文件夹路径
const folderPath1 = process.cwd() + "\\src\\mackdown";
// 递归遍历文件夹
const traverseFolder = (folderPath) => {

  const files = fs.readdirSync(folderPath);
  const filePaths = [];

  for (const file of files) {
    const fullPath = path.join(folderPath, file);
    const stats = fs.statSync(fullPath);

    if (stats.isDirectory()) {
      // 如果是文件夹，则递归调用
      filePaths.push(...traverseFolder(fullPath));
    } else if (path.extname(file) === '.md') {
      // 如果是以 .md 后缀的文件，则将其路径添加到结果数组中
      filePaths.push(fullPath);
    }
  }

  return filePaths;
}

// 调用函数并打印结果
const markdownFiles = traverseFolder(folderPath1);
const newarr = markdownFiles.map(item => "/src" + item.replace(/\\/g, "/").split('/src')[1])
// 将数组保存到 JSON 文件
const jsonContent = JSON.stringify(newarr, null, 4);

fs.writeFile('filePaths.json', jsonContent, 'utf8', (err) => {
  if (err) {
    console.error('保存文件时出错：', err);
  } else {
    console.log('markdown文件名已成功保存到 filePaths.json 文件。');
  }
});
